package com.orhon.smartcampus.modules.schoolaffairs.mutation;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.DictionaryDao;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Dictionary;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.schoolaffairs.InputMapper.SchoolNewsInputMapper;
import com.orhon.smartcampus.modules.schoolaffairs.InputMapper.SchoolNoticeInputMapper;
import com.orhon.smartcampus.modules.schoolaffairs.dao.SchoolNewsDao;
import com.orhon.smartcampus.modules.schoolaffairs.dao.SchoolNoticeDao;
import com.orhon.smartcampus.modules.schoolaffairs.entity.SchoolNews;
import com.orhon.smartcampus.modules.schoolaffairs.entity.SchoolNotice;
import com.orhon.smartcampus.modules.schoolaffairs.gqlInputModel.GQLSchoolNewsInput;
import com.orhon.smartcampus.modules.schoolaffairs.gqlInputModel.GQLSchoolNoticeInput;
import com.orhon.smartcampus.modules.student.dao.EclassDao;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentsDao;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Component
public class AddSchoolNoticeMutation implements DataFetcher {

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    @Autowired
    SchoolNoticeDao schoolNoticeDao;

    @Autowired
    EclassDao eclassDao;

    @Autowired
    UsersDao usersDao;

    @Autowired
    OrgDepartmentsDao orgDepartmentsDao;

    @Autowired
    DictionaryDao dictionaryDao;

    @Override
    @Transactional(readOnly = false)
    public Object get(DataFetchingEnvironment environment) throws Exception {
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        Long userId = JwtUtils.getUserId(token);
        GQLSchoolNoticeInput input = SchoolNoticeInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
        SchoolNotice schoolNotice=null;
        if (input.getId() != null){
            schoolNotice = schoolNoticeDao.get(SchoolNotice.class , input.getId());
            schoolNotice.setUpdatedAt(new Date());
        }
        else{
            schoolNotice = new SchoolNotice();
            schoolNotice.setCreatedAt(new Date());
        }
        if(input.getContent()!=null){
            schoolNotice.setContent(input.getContent());
        }
        if(input.getTitle()!=null){
            schoolNotice.setTitle(input.getTitle());
        }
        if (schoolId != null){
            Schools schools = schoolsDao.get(Schools.class ,schoolId);
            schoolNotice.setSchoolsBySchool(schools);
        }

        if(userId!=null){
            schoolNotice.setPublisher(usersDao.get(Users.class,userId));
        }
        if(input.getEclass_id()!=null){
            schoolNotice.setEclass(eclassDao.get(Eclass.class,input.getEclass_id()));
        }
        if(input.getUrl()!=null){
            schoolNotice.setUrl(input.getUrl());
        }
        if(input.getDepartments_id()!=null){
            schoolNotice.setDepartment(orgDepartmentsDao.get(OrgDepartments.class,input.getDepartments_id()));
        }
        if(input.getType_id()!=null){
            schoolNotice.setType(dictionaryDao.get(Dictionary.class,input.getType_id()));
        }
        try{
            schoolNoticeDao.saveOrUpdate(schoolNotice);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}
